/**
 * 获取本地图
 * @param name // 文件名 如 doc.png
 * @returns {*|string}
 */
export const getAssetsImages = (dir: String, name: String) => {
  return new URL(`/src/assets/${dir}/${name}`, import.meta.url).href
}

/**
 * 用于判断当前终端 (ios/android/pc)
 * @returns client: 终端，isIOS: 是否ios端
 */
export const judgeClient = () => {
  let isIOS = false
  let client = ""
  if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
    // 判断iPhone|iPad|iPod|iOS
    client = "iOS"
    isIOS = true
  } else if (/(Android)/i.test(navigator.userAgent)) {
    // 判断Android
    client = "Android"
    isIOS = false
  } else {
    client = "PC"
    isIOS = false
  }
  return { client, isIOS }
}
// 近N天 - new Date()
export const getRecentDay_Date = (n: number) => {
  var dd = new Date()
  dd.setDate(dd.getDate() + n) //获取n天后的日期
  var y = dd.getFullYear()
  var m: any = dd.getMonth() + 1 //获取当前月份的日期
  var d: any = dd.getDate()
  m = m < 10 ? "0" + m : m
  d = d < 10 ? "0" + d : d
  let day = y + "-" + m + "-" + d

  return day
}
// 近N月 - new Date()
export const getRecentMonth_Date = (n: number) => {
  let result = ""
  const datenow = new Date()

  let endMounth: any = datenow.getMonth() + 1
  let endday: any = datenow.getDate()
  endMounth = endMounth < 10 ? "0" + endMounth : endMounth
  endday = endday < 10 ? "0" + endday : endday
  const dateend = datenow.getFullYear().toString() + "-" + endMounth + "-" + endday
  datenow.setMonth(datenow.getMonth() - n)
  let dyear = datenow.getFullYear()
  let dmonth: any = datenow.getMonth() + 1

  dmonth = dmonth < 10 ? "0" + dmonth : dmonth
  let dday: any = datenow.getDate()
  dday = dday < 10 ? "0" + dday : dday
  const datestart = dyear + "-" + dmonth + "-" + dday
  result += datestart + ","
  result += dateend

  return datestart
}
export const HXtimeSlotChange = (val: string) => {
  let startTime, endTime
  let now = new Date() //当前日期
  var nowDayOfWeek = now.getDay() //今天本周的第几天
  var nowDay = now.getDate() //当前日
  let nowMonth = now.getMonth() //当前月
  let nowYear = now.getFullYear() //当前年
  let jd = Math.ceil((nowMonth + 1) / 3)
  switch (val) {
    case "本周":
      startTime = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek)
      endTime = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek))
      break
    case "本月":
      startTime = new Date(nowYear, nowMonth, 1)
      endTime = new Date(nowYear, nowMonth + 1, 0)
      break
    case "本季度":
      startTime = new Date(nowYear, (jd - 1) * 3, 1)
      endTime = new Date(nowYear, jd * 3, 0)
      break
    case "本年":
      startTime = new Date(nowYear, 0, 1)
      endTime = new Date(nowYear, nowMonth, nowDay)
      break
  }
  //格式化日期：yyyy-MM-dd
  function formatDate(date: any) {
    var myyear = date.getFullYear()
    var mymonth = date.getMonth() + 1
    var myweekday = date.getDate()
    if (mymonth < 10) {
      mymonth = "0" + mymonth
    }
    if (myweekday < 10) {
      myweekday = "0" + myweekday
    }
    return myyear + "-" + mymonth + "-" + myweekday
  }
  return {
    startTime: formatDate(startTime),

    endTime: formatDate(endTime)
  }
}
export function timestampToTime(dataString: string | number) {
  //dataString是整数，否则要parseInt转换
  var time = new Date(dataString)
  var year = time.getFullYear()
  var month = time.getMonth() + 1
  var day = time.getDate()
  var hour = time.getHours()
  var minute = time.getMinutes()
  var second = time.getSeconds()
  return (
    year +
    "-" +
    (month < 10 ? "0" + month : month) +
    "-" +
    (day < 10 ? "0" + day : day) +
    " " +
    (hour < 10 ? "0" + hour : hour) +
    ":" +
    (minute < 10 ? "0" + minute : minute)
    // ":" +
    // (second < 10 ? "0" + second : second)
  )
}
